/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzillahelp.tool;

import com.lonnesol.swing.button.IconButton;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.table.TableCellEditor;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author hualun-alan
 */
public class SwingTool {

    public static void cancelTableEditing(JTable jTable) {
        TableCellEditor cellEditor = jTable.getCellEditor();
        if (cellEditor != null) {
            cellEditor.stopCellEditing();
        }
    }

    public static JButton getJButton(String text, String actionCommand, ActionListener actionListener) {
        JButton jButton = new JButton(text);
        jButton.setActionCommand(actionCommand);
        jButton.addActionListener(actionListener);
        return jButton;
    }

    public static JButton getJButton(ImageIcon icon, ImageIcon rolloverIcon, ImageIcon pressedIcon, String actionCommand, ActionListener actionListener) {
        JButton jButton = new JButton(icon);
        jButton.setRolloverIcon(rolloverIcon);
        jButton.setPressedIcon(pressedIcon);
        jButton.setBorderPainted(false);
        jButton.setBackground(Color.white);
        jButton.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
        jButton.setActionCommand(actionCommand);
        jButton.addActionListener(actionListener);
        return jButton;
    }

    public static JButton getIconButton(String text, String actionCommand, ActionListener actionListener) {
        IconButton iconButton = new IconButton(text);
        iconButton.setActionCommand(actionCommand);
        iconButton.addActionListener(actionListener);
        return iconButton;
    }

    public static JPanel createEmptyPanel(Dimension d) {
        JPanel jPanel = new JPanel();
        jPanel.setPreferredSize(d);
        return jPanel;
    }

    //<editor-fold defaultstate="collapsed" desc="JTree相关">
    /**
     * 完全展开一个JTree
     *
     * @param tree JTree
     */
    public static void expandTree(JTree tree) {
        TreeNode root = (TreeNode) tree.getModel().getRoot();
        expandAll(tree, new TreePath(root), true);
    }

    /**
     * 完全展开或关闭一个树,用于递规执行
     *
     * @param tree JTree
     * @param parent 父节点
     * @param expand 为true则表示展开树,否则为关闭整棵树
     */
    private static void expandAll(JTree tree, TreePath parent, boolean expand) {
        // Traverse children
        TreeNode node = (TreeNode) parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {
            for (Enumeration e = node.children(); e.hasMoreElements();) {
                TreeNode n = (TreeNode) e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                expandAll(tree, path, expand);
            }
        }

        // Expansion or collapse must be done bottom-up
        if (expand) {
            tree.expandPath(parent);
        } else {
            tree.collapsePath(parent);
        }
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="JTabbedPane相关">
    /**
     * 返回给定Title的Tab索引
     *
     * @param jTabbedPane
     * @param title
     * @return
     */
    public static int getTabIndexByTitle(JTabbedPane jTabbedPane, String title) {
        int tabIndex = -1;

        if (jTabbedPane == null) {
            tabIndex = -1;
        } else {
            int tabCount = jTabbedPane.getTabCount();
            for (int i = 0; i < tabCount; i++) {
                String titleAt = jTabbedPane.getTitleAt(i);
                if (titleAt.equals(title)) {
                    tabIndex = i;
                    break;
                }
            }
        }

        return tabIndex;
    }

    /**
     * 返回第一个具有给的Title前缀的Tab索引
     *
     * @param jTabbedPane
     * @param titlePrefix
     * @return
     */
    public static int getTabIndexByStarting(JTabbedPane jTabbedPane, String titlePrefix) {
        int tabIndex = -1;

        if (jTabbedPane == null) {
            tabIndex = -1;
        } else {
            int tabCount = jTabbedPane.getTabCount();
            for (int i = 0; i < tabCount; i++) {
                String titleAt = jTabbedPane.getTitleAt(i);
                if (titleAt.startsWith(titlePrefix)) {
                    tabIndex = i;
                    break;
                }
            }
        }

        return tabIndex;
    }

    /**
     * 根据Tab标题移除Tab
     *
     * @param jTabbedPane
     * @param title
     */
    public static void removeTabByTitle(JTabbedPane jTabbedPane, String title) {
        if (jTabbedPane == null || title == null) {
            return;
        }
        int tabIndex = getTabIndexByTitle(jTabbedPane, title);
        if (tabIndex != -1) {
            jTabbedPane.removeTabAt(tabIndex);
        }
    }

    /**
     * 在sourceTitle的后面插入一个名为title，组件内容为jComponent的Tab
     *
     * @param jTabbedPane
     * @param title
     * @param jComponent
     * @param sourceTitle
     */
    public static void insertTabAfter(JTabbedPane jTabbedPane, String title, JComponent jComponent, String sourceTitle) {
        int idx1 = getTabIndexByTitle(jTabbedPane, title);
        if (idx1 == -1) {
            int idx2 = getTabIndexByTitle(jTabbedPane, sourceTitle);
            jTabbedPane.insertTab(title, null, jComponent, title, idx2 + 1);
        }
    }

    /**
     * 判断给的Title的Tab是否处于选中状态
     *
     * @param jTabbedPane
     * @param title
     * @return
     */
    public static boolean isTabSelected(JTabbedPane jTabbedPane, String title) {
        int selectedIndex = jTabbedPane.getSelectedIndex();
        int tabIndexByTitle = getTabIndexByTitle(jTabbedPane, title);
        return selectedIndex == tabIndexByTitle;
    }
    //</editor-fold>
}
